package com.hancher.gamelife2.birthday;

import android.content.Context;
import android.graphics.Bitmap;

import androidx.annotation.NonNull;

import com.hancher.common.androidutil.AsyncUtils;
import com.hancher.common.androidutil.ImageUtil;
import com.hancher.common.androidutil.LogUtil;
import com.hancher.common.androidutil.PathUtil;
import com.hancher.common.androidutil.ToastUtil;
import com.hancher.common.rv.PageRecyclerViewManager;
import com.hancher.gamelife2.R;
import com.hancher.gamelife2.databinding.RecyclerviewContainerBinding;
import com.lxj.xpopup.core.CenterPopupView;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.rxjava3.core.ObservableOnSubscribe;

/**
 * 描述 : 找人脸弹窗 <br/>
 * 时间 : 2024/1/15 11:53 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class SelectFaceDialog extends CenterPopupView {

    private final OnSelectFaceListener listener;
    private File file;
    private RecyclerviewContainerBinding binding;
    private PageRecyclerViewManager<Bitmap> prvm;

    interface OnSelectFaceListener {
        void onSelect(String uploadFinishVO);
    }


    public SelectFaceDialog(@NonNull Context context, File file, OnSelectFaceListener listener) {
        super(context);
        this.file = file;
        this.listener = listener;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = RecyclerviewContainerBinding.bind(getPopupImplView());
        clipFaceImage(file);

    }

    /**
     * 剪裁头像
     * @param file 文件
     */
    private void clipFaceImage(File file) {
        LogUtil.d("3. 开始选择人脸 文件位置:%s,是否存在%b", file.getAbsolutePath(), file.exists());
        if (!file.exists()) {
            return;
        }
        AsyncUtils.run((ObservableOnSubscribe<List<Bitmap>>) emitter -> {
            LogUtil.d("4. 图片人脸寻找");
            Bitmap bitmap = ImageUtil.file2Bitmap(file.getAbsolutePath());
            Bitmap[] faces = ImageUtil.getAllFaceBitmapByClip(bitmap);
            if (faces == null || faces.length == 0) {
                emitter.onNext(new ArrayList<>());
                emitter.onComplete();
                return;
            }
            emitter.onNext(Arrays.asList(faces.clone()));
            emitter.onComplete();
        }, bitmaps -> {
            if (bitmaps.size() == 0) {
                ToastUtil.show("5. 没找到人脸");
                return;
            }
            LogUtil.d("6. 弹窗选择人脸");

            prvm = new PageRecyclerViewManager<Bitmap>()
                    .setContext(getContext())
                    .setRecyclerView(binding.recyclerview)
                    .setAdapter(new SelectFaceImageAdapter())
                    .setCol(4)
                    .setOnItemClickListener((baseQuickAdapter, view, i) -> {
                        try {
                            String path = ImageUtil.saveBitmap(prvm.getRvList().get(i), PathUtil.OUT_APP_CACHE, true);
                            listener.onSelect(path);
                            dismiss();
                        } catch (Exception e) {
                            LogUtil.e("err:", e);
                        }
                    })
                    .init();
            ;
            prvm.freshData(bitmaps);

        });
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.recyclerview_container;
    }
}
